//
//  VVMAnchorTypesettingLayout.m
//  VVProjectHelper
//
//  Created by admin on 2022/4/29.
//

#import "VVMAnchorTypesettingLayout.h"

@implementation VVMAnchorTypesettingLayout

- (void)prepareLayout {
    [super prepareLayout];
    self.collectionView.scrollEnabled = NO;
}
- (NSInteger)supportNumber {
    NSInteger number = [self.collectionView numberOfItemsInSection:0];

    if (VVMAnchorTypesettingOne == self.type) {
        return 1;
    } else if (VVMAnchorTypesettingOneByOverThree == self.type) {
        return MIN(4, number);
    } else if (VVMAnchorTypesettingOneAndOne == self.type) {
        return MIN(2, number);
    } else if (VVMAnchorTypesettingOneAndTwo == self.type) {
        return MIN(3, number);
    } else if (VVMAnchorTypesettingTwoAndTwo == self.type) {
        return MIN(4, number);
    } else if (VVMAnchorTypesettingThreeByTwo == self.type) {
        return MIN(6, number);
    } else if (VVMAnchorTypesettingThreeByThree == self.type) {
        return MIN(9, number);
    }
    
    return number;
}
- (NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSArray *tmp = [super layoutAttributesForElementsInRect:rect];
    NSArray *array = [[NSArray alloc] initWithArray:tmp copyItems:YES];
    [self layoutWithType:self.type array:array];
    NSInteger number = [self supportNumber];
    NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(UICollectionViewLayoutAttributes *  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        return evaluatedObject.indexPath.row < number;
    }];
    return [array filteredArrayUsingPredicate:predicate];
}
- (CGSize)collectionViewContentSize {
    return self.collectionView.bounds.size;
}
- (CGSize)contentSize {
    //375 * 330
    CGFloat width = self.collectionView.bounds.size.width;
    
    return CGSizeMake(width, width * 330/375);
}
- (void)layoutWithType:(VVMAnchorTypesettingType)type array:(NSArray *)array{
    CGRect rect = self.collectionView.bounds;
    if (VVMAnchorTypesettingOne == type) {
        for (UICollectionViewLayoutAttributes *attr in array) {
            attr.frame = rect;
        }
    } else if (VVMAnchorTypesettingOneByOverThree == type) {
        CGFloat height = self.contentSize.height/3;
        CGSize itemSize = CGSizeMake(height * 84/112, height);
        for (UICollectionViewLayoutAttributes *attr in array) {
            if ([attr.indexPath isEqual:[NSIndexPath indexPathForRow:0 inSection:0]]) {
                attr.frame = rect;
            } else {
                attr.frame = CGRectMake(self.contentSize.width - itemSize.width,
                                        self.top + itemSize.height * (attr.indexPath.row - 1),
                                        itemSize.width,
                                        itemSize.height);

            }
        }
    } else if (VVMAnchorTypesettingOneAndOne == type) {
        CGSize itemSize = CGSizeMake(self.contentSize.width/2, self.contentSize.height);
        for (UICollectionViewLayoutAttributes *attr in array) {
            attr.frame = CGRectMake(itemSize.width * attr.indexPath.row,
                                    self.top,
                                    itemSize.width,
                                    itemSize.height);
        }
    } else if (VVMAnchorTypesettingOneAndTwo == type) {

        for (UICollectionViewLayoutAttributes *attr in array) {
            if ([attr.indexPath isEqual:[NSIndexPath indexPathForRow:0 inSection:0]]) {
                CGSize itemSize = CGSizeMake(self.contentSize.width/2, self.contentSize.height);
                attr.frame = CGRectMake(0, self.top, itemSize.width, itemSize.height);
            } else {
                CGSize itemSize = CGSizeMake(self.contentSize.width/2, self.contentSize.height/2);
                NSInteger row = attr.indexPath.row - 1;
                attr.frame = CGRectMake(itemSize.width,
                                        self.top + itemSize.height * row,
                                        itemSize.width,
                                        itemSize.height);
            }
        }
    } else if (VVMAnchorTypesettingTwoAndTwo == type) {
        CGSize itemSize = CGSizeMake(self.contentSize.width/2, self.contentSize.height/2);

        for (UICollectionViewLayoutAttributes *attr in array) {
            NSInteger row = floorf(attr.indexPath.row/2);
            NSInteger column = attr.indexPath.row%2;

            attr.frame = CGRectMake(itemSize.width * column,
                                    self.top + itemSize.height * row,
                                    itemSize.width,
                                    itemSize.height);
        }
    } else if (VVMAnchorTypesettingThreeByTwo == type
               || VVMAnchorTypesettingThreeByThree == type) {
        NSInteger m = 3;
        NSInteger n = VVMAnchorTypesettingThreeByTwo == type? 2:3;
        
        CGSize itemSize = CGSizeMake(self.contentSize.width/m, self.contentSize.height/n);

        for (UICollectionViewLayoutAttributes *attr in array) {
            NSInteger row = floorf(attr.indexPath.row/m);
            NSInteger column = attr.indexPath.row%m;

            attr.frame = CGRectMake(itemSize.width * column,
                                    self.top + itemSize.height * row,
                                    itemSize.width,
                                    itemSize.height);
        }
        NSInteger row = floorf(array.count/m);
        NSArray *lastRow = [array subarrayWithRange:NSMakeRange(row * m, array.count - row * m)];
        CGFloat total = itemSize.width * lastRow.count;
        CGFloat unused = CGRectGetWidth(self.collectionView.bounds) - total;
        CGFloat offset = unused/2;
        //修正最后一行offset
        for (UICollectionViewLayoutAttributes *attr in array) {
            if (attr.indexPath.row >= row * m) {
                attr.frame = CGRectOffset(attr.frame, offset, 0);
            }
        }

    }
}
- (void)setType:(VVMAnchorTypesettingType)type {
    _type = type;
    [self invalidateLayout];
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return YES;
}
@end
